package cn.gov.pbc.gd.credit.center.service.charge.impl;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.tools.LogUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.center.service.charge.ICCChargeService;
import cn.gov.pbc.gd.credit.center.service.util.JsoupUtil;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.ConfConstant;
import cn.gov.pbc.gd.credit.vo.center.charge.ReqCCChargeVo;
import cn.gov.pbc.gd.credit.vo.center.charge.ResCCChargeVo;

@Service
public class CCChargeServiceImpl implements ICCChargeService {

	private Logger logger = LoggerFactory.getLogger(CCChargeServiceImpl.class);
	
	@Override
	public ResultVo<ResCCChargeVo> charge(ReqCCChargeVo vo, HttpServletRequest request) {

		ResultVo<ResCCChargeVo> resultVo = new ResultVo<>();
		
		if(vo == null || StrUtil.isNull(vo.getIdno(), vo.getUser())){
			
			logger.error(LogUtil.msg("CCChargeServiceImpl:charge", "请求参数不正确", JSON.toJSONString(vo)));
			resultVo.setCode(CodeConstant.REQ_PARAM_FAIL);
			resultVo.setMsg("用户缴费，请求参数不正确");
			return resultVo;
		}
		
		vo.setForwardtype("1");
		String data = vo.toString();
		logger.debug(LogUtil.msg("CCChargeServiceImpl:charge", "用户缴费请求数据：" + data));
		String requestUrl = ConfConstant.ccDomainUrl + ConfConstant.ccQueryAndChargeUrl;
		logger.debug(LogUtil.msg("CCChargeServiceImpl:charge", "用户缴费请求URL：" + requestUrl));
		try {
			String res = "";
			if(StrUtil.isNotNull(ConfConstant.ccTestPath)){
				
				res = "1HG2017071400000115327898";				
			} else {
				
				//Document document = JsoupUtil.post(requestUrl, data, "GBK", vo.getCookies());
				res = JsoupUtil.postBody(requestUrl, data, "GBK", vo.getCookies());
				logger.debug(LogUtil.msg("CCChargeServiceImpl:charge", "用户缴费请求结果：" + res));
			}
			
			if(StrUtil.isNull(res)){
				
				logger.error(LogUtil.msg("CCChargeServiceImpl:charge", "征信中心收费接口返回数据为空"));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("征信中心收费接口返回数据为空");
				return resultVo;
			}
			
			String[] resArr = res.split("H");
			if(resArr[0] == "0"){
				logger.error(LogUtil.msg("CCChargeServiceImpl:charge", "缴费到征信中心失败:" + res));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("缴费到征信中心失败");
				return resultVo;
			}
			
			ResCCChargeVo resCCChargeVo = new ResCCChargeVo();
			
			resCCChargeVo.setChargeNo(resArr[1]);
			resultVo.setCode(CodeConstant.SUCCESS);
			resultVo.setData(resCCChargeVo);
			
		} catch (Exception e) {
			logger.error(LogUtil.msg("CCChargeServiceImpl:charge", "请求缴费接口异常"), e);
			resultVo.setCode(CodeConstant.FAIL);
			resultVo.setMsg("请求缴费接口异常");
			return resultVo;
		}
		
		return resultVo;
	}

	@Override
	public ResultVo<ResCCChargeVo> cancelCharge(ReqCCChargeVo vo, HttpServletRequest request) {

		ResultVo<ResCCChargeVo> resultVo = new ResultVo<>();
		
		if(vo == null || StrUtil.isNull(vo.getChargeNo())){
			
			logger.error(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "请求参数不正确"));
			resultVo.setCode(CodeConstant.REQ_PARAM_FAIL);
			resultVo.setMsg("用户缴费，请求参数不正确");
			return resultVo;
		}
		
		String data = vo.toCancelChargeStr();
		logger.debug(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "用户撤销缴费请求数据：" + data));
		String requestUrl = ConfConstant.ccDomainUrl + ConfConstant.ccQueryAndChargeUrl;
		logger.debug(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "用户撤销缴费请求URL：" + requestUrl));
		try {
			String res = "";
			if(StrUtil.isNotNull(ConfConstant.ccTestPath)){
				
				res = "1HG2017071400000115327898";				
			} else {
				
				//Document document = JsoupUtil.post(requestUrl, data, "GBK", vo.getCookies());
				res = JsoupUtil.postBody(requestUrl, data, "GBK", vo.getCookies());
				//res = document.html();
				logger.debug(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "用户撤销缴费请求结果：" + res));
			}
			
			if(StrUtil.isNull(res)){
				
				logger.error(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "征信中心撤销收费接口返回数据为空"));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("征信中心撤销收费接口返回数据为空");
				return resultVo;
			}
			
			String[] resArr = res.split("H");
			if(resArr[0] == "0"){
				logger.error(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "撤销缴费到征信中心失败:" + res));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("撤销缴费到征信中心失败");
				return resultVo;
			}

			resultVo.setCode(CodeConstant.SUCCESS);
					
		} catch (Exception e) {
			logger.error(LogUtil.msg("CCChargeServiceImpl:cancelCharge", "请求取消缴费接口异常"), e);
			resultVo.setCode(CodeConstant.FAIL);
			resultVo.setMsg("请求取消缴费接口异常");
			return resultVo;
		}
		
		return resultVo;
	}

}
